package com.itheima.test0909.test04;

import java.util.Random;
import java.util.Scanner;

public class BubbleSort {
    static Random random = new Random();
    public static void main(String[] args) {
//        int[] numbers = new int[]{9,5,8,2,3,1,4};
//        bubbleSortArray(numbers);
//        toStringArray(numbers);

        int ArrayLength = random.nextInt(20);
        int[] arr = new int[ArrayLength];
        arr = getRandomArray(arr);
        toStringArray(arr);
        bubbleSortArray(arr);
        toStringArray(arr);
    }

    private static void toStringArray(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + "  ");
        }
        System.out.println();
    }

    private static int[] getRandomArray(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int ArrayNumber = random.nextInt(100);
            arr[i] = ArrayNumber;
        }
        return arr;
    }

    private static void bubbleSortArray(int[] numbers) {
        for (int x = 0; x < numbers.length; x++) {
            for (int y = 0; y < numbers.length - x - 1; y++) {
                if (numbers[y] > numbers[y+1]){
                    int temp = numbers[y];
                    numbers[y] = numbers[y+1];
                    numbers[y+1] = temp;
                }
            }
        }
    }
}
